﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

using CPPEI.Coolzon.Common;
namespace CPPEI.Coolzon.Configuration.Server
{
    public class Config
    {
        public static HostConfig Host { get; private set; }

        /// <summary>
        /// 初始化服务的配置信息
        /// </summary>
        /// <param name="configFile"></param>
        public static void InitHostConfig(string configFile)
        {
            try
            {
                XDocument xConfig = XDocument.Load(configFile);

                XElement xHost = xConfig.Element("Host");

                List<ServiceConfig> scList = (from a in xHost.Descendants("Service")
                                              where a != null
                                              select new ServiceConfig
                                              {
                                                  Name = a.GetXAttributeValue("Name"),
                                                  Path = a.GetXAttributeValue("Path"),
                                                  Port = Convert.ToInt32(a.GetXAttributeValue("Port")),
                                                  IP = a.GetXAttributeValue("IP"),
                                                  MetadataGet = Convert.ToBoolean(a.GetXAttributeValue("MetadataGet")),
                                              }).ToList<ServiceConfig>();
                Host = new HostConfig();
                Host.ServiceList = scList;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
